#include <sxOpenGL_Stored_MeshRenderer.h>
#include <sxOpenGL.h>

Sx::OpenGL::Graphics::Objects::Rendering::OpenGL_Stored_MeshRenderer::OpenGL_Stored_MeshRenderer(const std::shared_ptr<OpenGL_Context>& graphicsContext) : 
	GraphicsObjectRenderer(graphicsContext){

}

Sx::OpenGL::Graphics::Objects::Rendering::OpenGL_Stored_MeshRenderer::~OpenGL_Stored_MeshRenderer() {}

bool Sx::OpenGL::Graphics::Objects::Rendering::OpenGL_Stored_MeshRenderer::construct(OpenGL_Context* const graphicsContext, Mesh* const mesh, StoredPhysicalRepresentation* const physicalRepresentation) {
	return false;
}

void Sx::OpenGL::Graphics::Objects::Rendering::OpenGL_Stored_MeshRenderer::update(OpenGL_Context* const graphicsContext, Mesh* const mesh, float dt) {
	return;
}

void Sx::OpenGL::Graphics::Objects::Rendering::OpenGL_Stored_MeshRenderer::render(OpenGL_Context* const graphicsContext, Mesh* const mesh, StoredPhysicalRepresentation* const physicalRepresentation) {
	
	
		glEnable(GL_BLEND);
		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
		glDisable(GL_CULL_FACE);
		glShadeModel (GL_FLAT);
		glDepthFunc(GL_LEQUAL);
	
	glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
	glColor4f(mesh->getColor().getR(), mesh->getColor().getG(), mesh->getColor().getB(), 0.2f);
	glBegin(GL_TRIANGLES);
		for ( unsigned int i = 0; i < mesh->getFaces().size(); i++ ) {
			for ( unsigned int x = 0; x < mesh->getFaces()[i].indices.size(); x++ ) {
				glVertex3dv(physicalRepresentation->vertices[mesh->getFaces()[i].indices[x]].data());
			}
		}
	glEnd();

	glEnable(GL_POLYGON_OFFSET_FILL);
	glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
	glPolygonOffset(1.0, 2);
	glColor4f(mesh->getColor().getR(), mesh->getColor().getG(), mesh->getColor().getB(), 0.4f);
	glBegin(GL_TRIANGLES);
		for ( unsigned int i = 0; i < mesh->getFaces().size(); i++ ) {
			for ( unsigned int x = 0; x < mesh->getFaces()[i].indices.size(); x++ ) {
				glVertex3dv(physicalRepresentation->vertices[mesh->getFaces()[i].indices[x]].data());
			}
		}
	glEnd();

	glDisable(GL_BLEND);
}
